package com.chen.manager.schedule;

import cn.hutool.core.date.DateUtil;
import com.chen.manager.mapper.OrderInfoMapper;
import com.chen.manager.mapper.OrderStatisticsMapper;
import com.chen.model.entity.order.OrderStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author cgj
 * @date 3/20/25 3:28 PM
 */
@Component
public class OrderTask {
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;
//    public static void main(String[] args) {
//        String date = DateUtil.offsetDay(new Date(), -1).toString(new SimpleDateFormat("yyyy-MM-dd"));
//        System.out.println(date);
//    }
//    @Scheduled(cron = "0 0 2 * * ?")
    //每天晚上2点执行
//@Scheduled(cron = "0/5 * * * * ?")
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics(){
        //拿到昨天的日期,  在凌晨2点执行查询order_info 分组统计订单的金额到 order_statistics
        String date = DateUtil.offsetDay(new Date(), -1).toString(new SimpleDateFormat("yyyy-MM-dd"));
        System.out.println(date);
        // 根据日期对查询, 结果可以封装到对应的实体类OrderStatistics
        OrderStatistics orderStatistics=orderInfoMapper.selectByDate(date);
        if(orderStatistics!=null){
            // 查到了昨天的数据, 把数据封装到表order_statistics 中
            orderStatisticsMapper.insertData(orderStatistics);
        }
    }
}
